package com.stone.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * MD5帮助类
 * @author Stone P
 *
 */
public class Md5Util {
	/**
	 * 将字符串MD5加密
	 * @param strSrc
	 * @return 
	 */
	public static String toMD5(String strSrc) {
                if(strSrc == null)
                    return "";
		char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'A', 'B', 'C', 'D', 'E', 'F' };
		byte[] bytes = strSrc.getBytes();
		MessageDigest md;
                try {
                    md = MessageDigest.getInstance("MD5");
                    md.update(bytes);
                    bytes = md.digest();
                    int j = bytes.length;
                    char[] chars = new char[j * 2];
                    int k = 0;
                    for (int i = 0; i < bytes.length; i++) {
                            byte b = bytes[i];
                            chars[k++] = hexChars[b >>> 4 & 0xf];
                            chars[k++] = hexChars[b & 0xf];
                    }
                    return new String(chars);
                } catch (NoSuchAlgorithmException ex) {
                    Logger.getLogger(Md5Util.class.getName()).log(Level.SEVERE, null, ex);
                }
		
		return "";
	}
}